package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;

import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.BusinessDTO;
import com.api.shopapi.entity.dto.BusinessDTO1;
import com.api.shopapi.entity.dto.BusinessStaffRelDTO;
import com.api.shopapi.entity.dto.UserDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * 商家
 * */


@RestController
@RequestMapping("Business")
public class BusinessController extends BaseController{

    @Autowired
    @Qualifier("BusinessServiceImplOne")
    private BusinessService businessService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @Autowired
    @Qualifier("UserAppInfoServiceImplOne")
    private UserAppInfoService userAppInfoService;



    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("BusinessStaffRelServiceImplOne")
    private BusinessStaffRelService businessStaffRelService;





    //政府管理员获取面板数据
    @RequestMapping(value = "Pannel", method = RequestMethod.POST)
    public MyResult Pannel(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");


            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Business/Pannel");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);
            String governmentCode = staff.getGovernmentCode();

            List<BusinessDTO> listBusiness = businessService.getListBusinessDTOByGovernmentCode(governmentCode);


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("businessCount",listBusiness.size());

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Business/Pannel", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //列表 (编辑子商家)
    @RequestMapping(value = "ListSuper",method = RequestMethod.POST)
    public MyResult ListSuper(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Business/ListSuper");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            List<Business> listBusiness = businessService.getList();
            List<BusinessDTO> listBusinessDTO = new ArrayList<>();
            for(Business business:listBusiness){
                BusinessDTO businessDTO = mapper.map(business,BusinessDTO.class);

                User user = userService.getByUserCode(businessDTO.getUserCode());
                if(user!=null){
                    UserDTO userDTO = mapper.map(user,UserDTO.class);
                    businessDTO.setUserDTO(userDTO);
                    businessDTO.setBusinessName(userDTO.getName());

                    listBusinessDTO.add(businessDTO);
                }

            }

            return success(listBusinessDTO);

        }catch (Exception e){
            insertError("/Business/ListSuper",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //列表 (政府下的所有商家)
    @RequestMapping(value = "ListForGov",method = RequestMethod.POST)
    public MyResult ListForGov(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Business/ListForGov");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            List<BusinessDTO> listBusinessDTO =
                    businessService.getListBusinessDTOByGovernmentCode(staff.getGovernmentCode());
            for(BusinessDTO businessDTO:listBusinessDTO){

                User user = userService.getByUserCode(businessDTO.getUserCode());
                UserDTO userDTO = mapper.map(user,UserDTO.class);
                businessDTO.setUserDTO(userDTO);
                businessDTO.setBusinessName(userDTO.getName());

            }

            return success(listBusinessDTO);

        }catch (Exception e){
            insertError("/Business/ListForGov",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }




    //列表
    @RequestMapping(value = "PageSuper",method = RequestMethod.POST)
    public MyResult PageSuper(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");


//            if(!checkSignPC(timeStr,sign)){
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Business/PageSuper");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            PageInfo<BusinessDTO> pageInfoBusinessDTO = businessService.getPageInfoBusinessDTO(pageIndex,pageSize);
            List<BusinessDTO> listBusinessDTO = pageInfoBusinessDTO.getList();
            for(BusinessDTO businessDTO : listBusinessDTO){

                User user = userService.getByUserCode(businessDTO.getUserCode());
                UserDTO userDTO = mapper.map(user,UserDTO.class);
                businessDTO.setUserDTO(userDTO);

                List<BusinessStaffRel> listBusinessStaffRel =
                        businessStaffRelService.getListByBusinessCode(businessDTO.getBusinessCode());
                List<BusinessStaffRelDTO> listBusinessStaffRelDTO = new ArrayList<>();
                for(BusinessStaffRel businessStaffRel:listBusinessStaffRel){
                    BusinessStaffRelDTO businessStaffRelDTO = mapper.map(businessStaffRel,BusinessStaffRelDTO.class);

                    Business business = businessService.getByBusinessCode(businessStaffRelDTO.getBusinessCode());
                    User user2 = userService.getByUserCode(business.getUserCode());
                    businessStaffRelDTO.setBusinessName(user2.getName());

                    Staff staff = staffService.getByStaffCode(businessStaffRelDTO.getStaffCode());
                    businessStaffRelDTO.setStaffName(staff.getLoginName());

                    listBusinessStaffRelDTO.add(businessStaffRelDTO);
                }
                businessDTO.setListBusinessStaffRelDTO(listBusinessStaffRelDTO);

            }

            return success(pageInfoBusinessDTO);

        }catch (Exception e){
            insertError("/Business/PageSuper",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //列表
    @RequestMapping(value = "PageGov",method = RequestMethod.POST)
    public MyResult PageGov(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");


            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Business/PageGov");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff2 = staffService.getByStaffCode(staffCode);
            String governmentCode = staff2.getGovernmentCode();
            PageInfo<BusinessDTO> pageInfoBusinessDTO = businessService.getPageInfoBusinessDTOByGovernmentCode(pageIndex,
                    pageSize, governmentCode);

            List<BusinessDTO> listBusinessDTO = pageInfoBusinessDTO.getList();
            for(BusinessDTO businessDTO : listBusinessDTO){

                User user = userService.getByUserCode(businessDTO.getUserCode());
                UserDTO userDTO = mapper.map(user,UserDTO.class);
                businessDTO.setUserDTO(userDTO);

                List<BusinessStaffRel> listBusinessStaffRel =
                        businessStaffRelService.getListByBusinessCode(businessDTO.getBusinessCode());
                List<BusinessStaffRelDTO> listBusinessStaffRelDTO = new ArrayList<>();
                for(BusinessStaffRel businessStaffRel:listBusinessStaffRel){
                    BusinessStaffRelDTO businessStaffRelDTO = mapper.map(businessStaffRel,BusinessStaffRelDTO.class);

                    Business business = businessService.getByBusinessCode(businessStaffRelDTO.getBusinessCode());
                    User user2 = userService.getByUserCode(business.getUserCode());
                    businessStaffRelDTO.setBusinessName(user2.getName());

                    Staff staff = staffService.getByStaffCode(businessStaffRelDTO.getStaffCode());
                    businessStaffRelDTO.setStaffName(staff.getLoginName());

                    listBusinessStaffRelDTO.add(businessStaffRelDTO);
                }
                businessDTO.setListBusinessStaffRelDTO(listBusinessStaffRelDTO);

            }

            return success(pageInfoBusinessDTO);

        }catch (Exception e){
            insertError("/Business/PageGov",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //添加
    @RequestMapping(value = "AddGov",method = RequestMethod.POST)
    @Transactional
    public MyResult AddGov(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String phone = jsonObject.getString("phone");
            String password = jsonObject.getString("password");
            String name = jsonObject.getString("name");
            String address = jsonObject.getString("address");
            int nature=jsonObject.getInteger("nature");
            String principal =jsonObject.getString("principal");
            String legalPerson=jsonObject.getString("legalPerson");
            String serviceContent =jsonObject.getString("serviceContent");
            String startTime=jsonObject.getString("startTime");
            String endTime=jsonObject.getString("endTime");
            String fixedPhone=jsonObject.getString("fixedPhone");
            String serviceRegion=jsonObject.getString("serviceRegion");
//            int publishRegion=jsonObject.getInteger("publishRegion");
            String idPhoto=jsonObject.getString("idPhoto");
            String businessImg=jsonObject.getString("businessImg");
            String licenseCode=jsonObject.getString("licenseCode");
            int closedCircle=jsonObject.getInteger("closedCircle");
            String appendix=jsonObject.getString("appendix");
            String businessIntroduce=jsonObject.getString("businessIntroduce");
            String weekendService=jsonObject.getString("weekendService");
            String holidayService=jsonObject.getString("holidayService");

            //String zoneCode = jsonObject.getString("zoneCode");
            //String zoneName = "";


            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Business/AddGov");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);


            User user = userService.getByPhone(phone);
            if(user != null){
                return failed("手机号已注册");
            }

//            Zone zone = zoneService.getByZoneCode(zoneCode);
//            if(zone != null){
//                zoneName = zone.getName();
//            }


            user = new User();
            user.setUserCode(UUID.randomUUID().toString());
            user.setPhone(phone);
            user.setPassword(getMD5Str(password));
            user.setName(name);
            user.setCreateTime(new Date());
            user.setUserType(3);
            user.setStaffCode(staffCode);
            user.setGovernmentCode(staff.getGovernmentCode());
            userService.insert(user);


            Business business = new Business();
            business.setBusinessCode(UUID.randomUUID().toString());
            business.setUserCode(user.getUserCode());
            business.setAddress(address);
            business.setGovernmentCode(staff.getGovernmentCode());
            //business.setZoneCode(zoneCode);
            //business.setZoneName(zoneName);
            business.setAuditStatus(1);
            business.setPhone(phone);
            business.setNature(nature);
            business.setPrincipal(principal);
            business.setLegalPerson(legalPerson);
            business.setServiceContent(serviceContent);
            business.setStartTime(startTime);
            business.setEndTime(endTime);
            business.setFixedPhone(fixedPhone);
            business.setServiceRegion(serviceRegion);
//            business.setPublishRegion(publishRegion);
            business.setIdPhoto(idPhoto);
            business.setBusinessImg(businessImg);
            business.setLicenseCode(licenseCode);
            business.setClosedCircle(closedCircle);
            business.setAppendix(appendix);
            business.setBusinessIntroduce(businessIntroduce);
            business.setWeekendService(weekendService);
            business.setHolidayService(holidayService);
            businessService.insert(business);


            Object object = new Object[] { };
            return success(object);

        }catch (Exception e){
            insertErrorForAffair("/Business/AddGov!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }


    //编辑
    @RequestMapping(value = "EditGov",method = RequestMethod.POST)
    @Transactional
    public MyResult EditGov(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            String address = jsonObject.getString("address");
            //String zoneCode = jsonObject.getString("zoneCode");
            //int status = jsonObject.getInteger("status");
            //String zoneName = "";

            int nature=jsonObject.getInteger("nature");
            String principal =jsonObject.getString("principal");
            String legalPerson=jsonObject.getString("legalPerson");
            String serviceContent =jsonObject.getString("serviceContent");
            String startTime=jsonObject.getString("startTime");
            String endTime=jsonObject.getString("endTime");
            String fixedPhone=jsonObject.getString("fixedPhone");
            String serviceRegion=jsonObject.getString("serviceRegion");
//            int publishRegion=jsonObject.getInteger("publishRegion");
            String idPhoto=jsonObject.getString("idPhoto");
            String businessImg=jsonObject.getString("businessImg");
            String licenseCode=jsonObject.getString("licenseCode");
            int closedCircle=jsonObject.getInteger("closedCircle");
            String appendix=jsonObject.getString("appendix");
            String businessIntroduce=jsonObject.getString("businessIntroduce");
            String weekendService=jsonObject.getString("weekendService");
            String holidayService=jsonObject.getString("holidayService");

            String businessCode = jsonObject.getString("businessCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Business/EditGov");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

//            Zone zone = zoneService.getByZoneCode(zoneCode);
//            if(zone != null){
//                zoneName = zone.getName();
//            }


            Business business = businessService.getByBusinessCode(businessCode);
            if(business == null){
                return failed("商家不存在");
            }

            User user = userService.getByUserCode(business.getUserCode());
            if(business == null){
                return failed("用户不存在");
            }


            //business.setZoneCode(zoneCode);
            //business.setZoneName(zoneName);
            business.setAddress(address);
            business.setAuditStatus(1);
            business.setNature(nature);
            business.setPrincipal(principal);
            business.setLegalPerson(legalPerson);
            business.setServiceContent(serviceContent);
            business.setStartTime(startTime);
            business.setEndTime(endTime);
            business.setFixedPhone(fixedPhone);
            business.setServiceRegion(serviceRegion);
//            business.setPublishRegion(publishRegion);
            business.setIdPhoto(idPhoto);
            business.setBusinessImg(businessImg);
            business.setLicenseCode(licenseCode);
            business.setClosedCircle(closedCircle);
            business.setAppendix(appendix);
            business.setBusinessIntroduce(businessIntroduce);
            business.setWeekendService(weekendService);
            business.setHolidayService(holidayService);
            businessService.updateByBusinessCode(business);

            user.setName(name);
            userService.update(user);


            Object data = new Object[] { };
            return success(data);

        }catch (Exception e){
            insertErrorForAffair("/Business/EditGov!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }






    //above is for admin ============================================





    //登录
    @RequestMapping(value = "Login",method = RequestMethod.POST)
    public MyResult Login(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String phone = jsonObject.getString("phone");
            String password = jsonObject.getString("password");
            String os = jsonObject.getString("os");
            String regId = jsonObject.getString("regId");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Business/Login");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByPhoneAndPassword(phone,getMD5Str(password));
            if(user == null){
                return failed("用户名或者密码错误");
            }
            if(user.getStatus() == 2){
                return failed("账户被禁用");
            }
            if(user.getUserType() != 3){
                return failed("账户类型错误");
            }

            Business business = businessService.getByUserCode(user.getUserCode());


            UserAppInfo userAppInfo = userAppInfoService.getByUserCode(user.getUserCode());
            if(userAppInfo == null){
                userAppInfo = new UserAppInfo();
                userAppInfo.setUserAppInfoCode(UUID.randomUUID().toString());
                userAppInfo.setUserCode(user.getUserCode());
                userAppInfo.setAppOs(os);
                userAppInfo.setRegId(regId);
                userAppInfoService.insert(userAppInfo);
            }
            else{
                if(!regId.equals("")){
                    if(!os.equals(userAppInfo.getAppOs()) || !regId.equals(userAppInfo.getRegId())){
                        userAppInfo.setAppOs(os);
                        userAppInfo.setRegId(regId);
                        userAppInfoService.update(userAppInfo);
                    }
                }
            }



            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("user",user);
            jsonObject1.put("business",business);
            jsonObject1.put("userAppInfo",userAppInfo);

            return success(jsonObject1);

        }catch (Exception e){
            insertError("/Business/Login",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //退出
    @RequestMapping(value = "LoginOut",method = RequestMethod.POST)
    public MyResult LoginOut(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Business/LoginOut");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            UserAppInfo userAppInfo = userAppInfoService.getByUserCode(userCode);
            if(userAppInfo == null){
                return failed("用户不存在");
            }else{
                userAppInfo.setAppOs("");
                userAppInfo.setRegId("");
                userAppInfoService.update(userAppInfo);
            }

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);

        }catch (Exception e){
            insertError("/Business/LoginOut",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //检查登录
    @RequestMapping(value = "CheckLogin",method = RequestMethod.POST)
    public MyResult CheckLogin(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");
            String os = jsonObject.getString("os");
            String regId = jsonObject.getString("regId");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Business/CheckLogin");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            UserAppInfo userAppInfo = userAppInfoService.getByUserCode(userCode);
            if(userAppInfo == null){
                return failed("用户不存在");
            }

            Boolean loginOut = false;
            if(!userAppInfo.getAppOs().equals(os) || !userAppInfo.getRegId().equals(regId)){
                loginOut = true;
            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("loginOut",loginOut);
            return success(jsonObject1);

        }catch (Exception e){
            insertError("/Business/CheckLogin",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }



    //商家已完成订单统计
    @RequestMapping(value = "BusinessOrderStatistics",method = RequestMethod.POST)
    public MyResult BusinessOrderStatistics(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            int pageIndex = jsonObject.getIntValue("pageIndex");
            int pageSize = jsonObject.getIntValue("pageSize");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Business/BusinessOrderStatistics");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Staff staff = staffService.getByStaffCode(staffCode);
            List<BusinessDTO1> businessDTO1List=new ArrayList<>();
            PageInfo<BusinessDTO1> businessDTO1PageInfo = businessService.getPageForGov(pageIndex, pageSize, staff.getGovernmentCode());
            for (BusinessDTO1 businessDTO1 : businessDTO1PageInfo.getList()) {
                BusinessDTO1 newBusinessDTO1 = businessService.businessOrderStatistics(businessDTO1.getBusinessCode(), startTime, endTime);
                if (newBusinessDTO1!=null){
                    User user = userService.getByUserCode(newBusinessDTO1.getUserCode());
                    newBusinessDTO1.setBusinessName(user.getName());
                    businessDTO1List.add(newBusinessDTO1);
                }else {
                    User user = userService.getByUserCode(businessDTO1.getUserCode());
                    businessDTO1.setBusinessName(user.getName());
                    businessDTO1List.add(businessDTO1);
                }
            }
            businessDTO1PageInfo.setList(businessDTO1List);

            return success(businessDTO1PageInfo);
        }catch (Exception e){
//            e.printStackTrace();
            insertError("/Business/BusinessOrderStatistics",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //统计商家本年度的每个月订单数和订单量
    @RequestMapping(value = "BusinessYearData",method = RequestMethod.POST)
    public MyResult BusinessYearData(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String businessCode = jsonObject.getString("businessCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Business/BusinessYearData");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            YearData businessYearData = businessService.getBusinessYearData(businessCode);

            return success(businessYearData);
        }catch (Exception e){
//            e.printStackTrace();
            insertError("/Business/BusinessYearData",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }



    //列表
    @RequestMapping(value = "PageToGovPlatform",method = RequestMethod.POST)
    public MyResult PageToGovPlatform(@RequestBody JSONObject jsonObject) {

        try{
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");
            String governmentCode = jsonObject.getString("governmentCode");


            PageInfo<BusinessDTO> pageInfoBusinessDTO = businessService.getPageInfoBusinessDTOByGovernmentCode(pageIndex,
                    pageSize,
                    governmentCode);

            //PageInfo<BusinessDTO> pageInfoBusinessDTO = businessService.getPageInfoBusinessDTO(pageIndex,pageSize);
            List<BusinessDTO> listBusinessDTO = pageInfoBusinessDTO.getList();
            for(BusinessDTO businessDTO : listBusinessDTO){

                User user = userService.getByUserCode(businessDTO.getUserCode());
                UserDTO userDTO = mapper.map(user,UserDTO.class);
                businessDTO.setUserDTO(userDTO);

                List<BusinessStaffRel> listBusinessStaffRel =
                        businessStaffRelService.getListByBusinessCode(businessDTO.getBusinessCode());
                List<BusinessStaffRelDTO> listBusinessStaffRelDTO = new ArrayList<>();
                for(BusinessStaffRel businessStaffRel:listBusinessStaffRel){
                    BusinessStaffRelDTO businessStaffRelDTO = mapper.map(businessStaffRel,BusinessStaffRelDTO.class);

                    Business business = businessService.getByBusinessCode(businessStaffRelDTO.getBusinessCode());
                    User user2 = userService.getByUserCode(business.getUserCode());
                    businessStaffRelDTO.setBusinessName(user2.getName());

                    Staff staff = staffService.getByStaffCode(businessStaffRelDTO.getStaffCode());
                    businessStaffRelDTO.setStaffName(staff.getLoginName());

                    listBusinessStaffRelDTO.add(businessStaffRelDTO);
                }
                businessDTO.setListBusinessStaffRelDTO(listBusinessStaffRelDTO);

            }

            return success(pageInfoBusinessDTO);

        }catch (Exception e){
            insertError("/Business/PageToGovPlatform",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }



    //列表 (政府下的所有商家)
    @RequestMapping(value = "ListToGovPlatform",method = RequestMethod.POST)
    public MyResult ListToGovPlatform(@RequestBody JSONObject jsonObject) {

        try{

            String governmentCode = jsonObject.getString("governmentCode");

            List<BusinessDTO> listBusinessDTO =
                    businessService.getListBusinessDTOByGovernmentCode(governmentCode);
            for(BusinessDTO businessDTO:listBusinessDTO){

                User user = userService.getByUserCode(businessDTO.getUserCode());
                UserDTO userDTO = mapper.map(user,UserDTO.class);
                businessDTO.setUserDTO(userDTO);
                businessDTO.setBusinessName(userDTO.getName());

            }


            return success(listBusinessDTO);

        }catch (Exception e){
            insertError("/Business/ListToGovPlatform",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }





}
